// Sunghan Cho
// 301140084
// sunghanc@sfu.ca

#include "itemmanager.h"
#include <QDebug>

ItemManager::ItemManager(QObject *parent) :
    QObject(parent)
{
}

ItemManager::~ItemManager()
{
}

void ItemManager::addItem(Item *pItem)
{
    m_items.append(pItem);
    emit itemsChanged();
    //qDebug() << "ItemManager::addItem(Item* pItem) emit itemsChanged()";
}

int ItemManager::getItemCount()
{
    /*
    int count = this->m_items.count();
    return count;
    */
    return this->m_items.count();
}

QListIterator<Item *> ItemManager::getIterator()
{
    QListIterator<Item*> itr(m_items);
    return itr;
}

Item* ItemManager::findMatchingItem(QVariant keyValue)
{
    foreach(Item* itr, m_items){
        if(itr->matchesPrimaryField(keyValue) == true){
            return itr;
        }
    }
    return 0;
}

void ItemManager::debugDumpData()
{
    foreach(Item* itr, m_items){
        if(itr->getFieldCount()==2){
            qDebug() << "Course = " << itr->getFieldValue(0).toString()
                     <<", Semester = " << itr->getFieldValue(1).toString();
        }

        else if(itr->getFieldCount()==3){
            qDebug() << "Name = " << itr->getFieldValue(0).toString()
                     << ", Student# = " << itr->getFieldValue(1).toInt()
                     << ", Courses = " << itr->getFieldValue(2).toString();
        }
    }
}
